/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package at.tugraz.hidipic.web.taglib;

import at.tugraz.hidipic.model.SubProject;
import at.tugraz.hidipic.model.User;
import at.tugraz.hidipic.web.KDDConstants;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.CollectionUtils;

/**
 *
 * @author mario
 */
public class KDDTagLib {

    public KDDTagLib() {
    }

    /**
     * check if the object is of the given class
     *
     * @param object
     * @param className
     * @return true if it is, otherwise false
     * @throws ClassNotFoundException
     */
    public static boolean isInstanceOf(Object object, String className) throws ClassNotFoundException {
        Class clazz = Class.forName(className);
        if (clazz.isAssignableFrom(object.getClass())) {
            return true;
        }
        return false;
    }

    /**
     * transform newlines to breaks in jsps
     *
     * @param s
     * @return
     */
    public static String nl2br(String s) {
        if (s != null) {
            return s.replace("\n", "<br />");
        } else {
            return null;
        }
    }

    /**
     * check if the subproject is followed by the user
     * @param user
     * @param subProject
     * @return true if it is, false otherwise
     */
    public static boolean isFollowProject(User user, SubProject subProject) {
        if (user != null && subProject != null) {
            Set<SubProject> subProjects = user.getFollowSubProjects();
            if (subProjects != null && subProjects.contains(subProject)) {
                return true;
            }
        }
        return false;
    }

    /**
     * checks if the menuPoint is active at the moment
     * @param menuName
     * @param requestUri
     * @return true if it is, otherwise false
     */
    public static boolean isActiveMenu(String menuName, String requestUri) {
        if (menuName != null && requestUri != null) {
            String[] urlParts = StringUtils.split(requestUri, "/");
            int length = urlParts.length;
            String menuPart = null;
            if (menuName.contains("/")) {
                if (length >= 3) {
                    menuPart = urlParts[1] + "/" + urlParts[2];
                    if (menuName.equals(menuPart)) {
                        return true;
                    }
                }
            } else {
                if (length >= 2) {
                    menuPart = urlParts[1];
                }
                if (menuName.equals(menuPart)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * checks if the user has the given role
     * @param user
     * @param role
     * @return true if it has, otherwise false
     * @throws ClassNotFoundException 
     */
    public static boolean hasRole(User user, String role) throws ClassNotFoundException {
        if (!CollectionUtils.isEmpty(user.getAuthorities())) {
            for (GrantedAuthority authority : user.getAuthorities()) {
                if (authority.getAuthority().equals(role)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * get a displayable user role for a user
     * @param user
     * @return returns the display-userrole
     * @throws ClassNotFoundException 
     */
    public static String getUserRole(User user) throws ClassNotFoundException {
        if (hasRole(user, KDDConstants.ADMIN_ROLE)) {
            return KDDConstants.ADMIN_ROLE_NAME;
        } else if (hasRole(user, KDDConstants.RESEARCHER_ROLE)) {
            return KDDConstants.RESEARCHER_ROLE_NAME;
        } else {
            return KDDConstants.USER_ROLE_NAME;
        }
    }

    /**
     * get the css class for the given type
     * @param type
     * @return the css-class string
     */
    public static String getTypeColor(Integer type) {
        if (type == 0) {
            return "alert-info";
        } else if (type == 1) {
            return "alert-danger";
        } else if (type == 2) {
            return "alert-success";
        }
        return "alert-info";
    }
}
